<?php
/**
 * 功能：json数据
 * 说明：一、本周工作动态(Operating Developments)
 * 创建日期：2011-12-07
 * 更新日期：
 * 作者：Mirze
 * 补充说明： 
 * 
 */
include('ThePage.class.php');

$db = Factory::getDB('zy_certificate_statistic');

//--- 查询日期 (周)---
//-------------------

$_search = ('true' == $_REQUEST['_search']) ? true : false; //是否搜索
$oper = isset($_REQUEST['oper']) ? trim($_REQUEST['oper']) : ''; //操作来源：add/edit/del

$sidx = isset($_REQUEST['sidx']) ? trim($_REQUEST['sidx']) : ''; //主键
$sord= ('desc' == $_REQUEST['sord']) ? 'desc' : 'asc'; //排序
$rows = isset($_REQUEST['rows']) ? intval($_REQUEST['rows']) : 5;  //取每页记录条数
$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;  //当前页码

$where = '1';
$col = $sidx;

/** 搜索 **/
if($_search) {
	$oper = 'search';
}

switch($oper) {
	case 'search':
		$searchField = isset($_REQUEST['searchField']) ? trim($_REQUEST['searchField']) : ''; //字段
		$searchString = isset($_REQUEST['searchString']) ? trim($_REQUEST['searchString']) : ''; //值
		$searchOper = isset($_REQUEST['searchOper']) ? trim($_REQUEST['searchOper']) : 'eq'; //查询条件：
		if('' != $searchField) {
			$where = "{$searchField} = '{$searchString}'";
		}

	    break;
	case 'add':
		$val['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
		$val['cs_type'] = isset($_REQUEST['cs_type']) ? trim($_REQUEST['cs_type']) : '';
		$val['cs_date'] = isset($_REQUEST['cs_date']) ? trim($_REQUEST['cs_date']) : '';
		$val['authority'] = isset($_REQUEST['authority']) ? trim($_REQUEST['authority']) : '';
		$val['validity'] = isset($_REQUEST['validity']) ? trim($_REQUEST['validity']) : '';
		$val['cs_address'] = isset($_REQUEST['cs_address']) ? trim($_REQUEST['cs_address']) : '';
		$val['info'] = isset($_REQUEST['info']) ? trim($_REQUEST['info']) : '';
		$val['dtype'] = isset($_REQUEST['dtype']) ? trim($_REQUEST['dtype']) : '';

		/* 隐藏数据 */
		$val['project'] = isset($_REQUEST['project']) ? intval($_REQUEST['project']) : 0;

		$week_num = isset($_REQUEST['week']) ? intval($_REQUEST['week']) : date('W'); 
		$create_user = isset($_REQUEST['create_user']) ? intval($_REQUEST['create_user']) : 0; 

		$val['create_time'] = date('Y-m-d');
		$val['week_time'] = date('Y') . '-' . $week_num; //周报时间：2011-42
		$val['add_user'] = $create_user;
		$val['department'] = get_user_dev($create_user);		

		$res = $db->insert_zy_certificate_statistic($val);
		//print_r($db->getDebug());
		if(empty($res)) {
			echo "<script>alert('添加失败');</script>";
		}
	    break;
	case 'edit':
	
	    break;
	case 'del':
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0; 
		$where = "cs_id = '{$id}'";
		$res = $db->delete_zy_certificate_statistic($where);
	    break;
	default:
		
		break;
}

if('asc' == $sord) {$db->setOrderBy();}
//查询项目和所属周
$project = isset($_GET['project']) ? intval($_GET['project']) : 0;
$week_num = isset($_GET['week']) ? intval($_GET['week']) : 0;
if($project){
	$where .= " and project={$project}";
}
if($week_num){
	$where .= " and week_time='".date('Y')."-".$week_num."'";
}
//获取分页信息
$json_arr = $db->getPageInfo($where, $page, $rows);
if(isset($json_arr['records']) && $json_arr['records'] > 0) {
	//获取当前页数据
	$offset = ($page-1) * $rows;	
	$datas = $db->getAll($where, $col, $rows, $offset);
	$list_data = is_array($datas) ? $datas : array();
} else {
    $list_data = array();
}
$json_arr['list_data'] = $list_data; //数据

echo json_encode($json_arr);

?>